import type { SoElements } from '../../../../../../../../types/constants/personalization/so'
import { ThemeVarService } from '../../../../../../../../lib/abstract/style/theme_var/service'
import {
  AppStyleCollections,
  generateHideSelector,
  PAGE_ELEMENTS,
  SUPPORTED_PLATFORMS,
} from '../../../../../../../../types/constants'
import { SearchCommonStyles } from '../../common'
import { SoCommonPage } from './common'

export class SoNewsPage {
  elements: SoElements = PAGE_ELEMENTS[SUPPORTED_PLATFORMS.SO]
  private static instance: SoNewsPage
  private readonly themeVarService: ThemeVarService = ThemeVarService.getInstance()
  private readonly searchCommonStyles: SearchCommonStyles = SearchCommonStyles.getInstance()
  private readonly commonPage: SoCommonPage = SoCommonPage.getInstance()

  /**
   * 获取单例实例
   */
  public static getInstance(): SoNewsPage {
    if (!SoNewsPage.instance) {
      SoNewsPage.instance = new SoNewsPage()
    }
    return SoNewsPage.instance
  }

  init(): void {
  }

  /**
   * 无搜索结果样式
   */
  noSearchResult(): string {
    return `
  /* 无搜索结果样式 */
  #head {
    ${this.commonPage.headerStyles()}
  }
  
  #wrapper #container #main {
    #no-result {
      display: flex !important;
      justify-content: center !important;
      align-items: center !important;
      
      .nors {
        color: var(${this.themeVarService.search.default.text}) !important;
        
        span {
          color: var(${this.themeVarService.search.default.text}) !important;
        }
      }
    }
  }  
    `
  }

  hideSearchStyles(): string {
    return `
    .so-search__input.has-sug {
      border-radius: 10px 0 0 10px !important;
      border-bottom: 2px solid #0fb264 !important;
    }
    
    .so-suggest {
      display: none !important;
    }
    `
  }

  getMainStyles(): string {
    return `
    /* 壁纸样式 */
    #${AppStyleCollections.Wallpaper.CONTAINER} {
      z-index: 0 !important;
    }
    &:has(#${AppStyleCollections.Wallpaper.CONTAINER}) {
      #so-nav-container {
        z-index: 1 !important;
        position: relative !important;
      }
      
      #wrapper #side {
        background-color: transparent !important;
      }
    }
    
    /* 隐藏样式 */
    &:has(${generateHideSelector('#filter')}) {
      #wrapper #container #news {
        margin-top: ${this.themeVarService.search.resultItem.gap} !important;
      }
    }
    &:has(${generateHideSelector('#so-nav-container')}):has(${generateHideSelector('#filter')}) {
      #wrapper #container #news {
        margin-top: 80px !important;
      }
    }
    &:has(${generateHideSelector('#so-nav-container')}):not(:has(${generateHideSelector('#filter')})) {
      #wrapper #container #news {
        margin-top: ${this.themeVarService.search.resultItem.gap} !important;
      }
    }
    
    /* 搜索历史隐藏、搜索建议隐藏 */
    &[${AppStyleCollections.Search.attr.HIDE_HISTORY}="true"][${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}="true"] {
      form.so-search__form {
        ${this.hideSearchStyles()}
      }
    }
    /* 搜索历史隐藏、搜索建议显示 */
    &[${AppStyleCollections.Search.attr.HIDE_HISTORY}="true"]:not([${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}]) {
      /* 有搜索历史、无搜索建议 */
      form.so-search__form[${AppStyleCollections.Search.attr.IS_HISTORY_EMPTY}="false"][${AppStyleCollections.Search.attr.IS_SUGGESTIONS_EMPTY}="true"] {
        ${this.hideSearchStyles()}
      }
      
      /* 热门搜索隐藏*/
      &[${AppStyleCollections.Search.attr.HIDE_HOT_LISTS}="true"] {
        /* 无搜索历史、无搜索建议 */
        form.so-search__form[${AppStyleCollections.Search.attr.IS_HISTORY_EMPTY}="true"][${AppStyleCollections.Search.attr.IS_SUGGESTIONS_EMPTY}="true"] {
          ${this.hideSearchStyles()}
        }
      }
    }
    /* 搜索历史显示、搜索建议隐藏 */
    &:not([${AppStyleCollections.Search.attr.HIDE_HISTORY}])[${AppStyleCollections.Search.attr.HIDE_SUGGESTIONS}="true"] {
      /* 无搜索历史 */
      form.so-search__form[${AppStyleCollections.Search.attr.IS_HISTORY_EMPTY}="true"] {
        ${this.hideSearchStyles()}
      }
    }
    
    ${this.noSearchResult()}
    
    /* 搜索建议高亮 */
    #head {
      .so-header__content {
        .so-header__left {
          .so-suggest {
            .so-suggest__item {
              /* 搜索历史高亮 */
              &.history {
                .so-suggest__text {
                  color: var(${this.themeVarService.search.history.text}) !important;
                    
                  &:has(> b) {
                    color: var(${this.themeVarService.em.text}) !important;
                  }
                  
                  b {
                    color: var(${this.themeVarService.search.history.text}) !important;
                  }
                }
              }
              
              /* 搜索建议高亮 */
              &.suggest {
                .so-suggest__text {
                  color: var(${this.themeVarService.search.suggestions.text}) !important;
                  
                  &:has(> b) {
                    color: var(${this.themeVarService.em.text}) !important;
                  }
                }
              }
            }
          }
        }
        
        .so-header__right {
          .so-header-menu {
            .so-header-menu-item__title {
              &.user_action {
                color: var(${this.themeVarService.search.default.text}) !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
              }

              a {
                color: var(${this.themeVarService.search.default.text}) !important;
                
                &:hover {
                  color: var(${this.themeVarService.link.default.text}) !important;
                }
              }
            }
          }
        }
      }
    }
    
    /* 主样式 */
    #wrapper {
      background-color: var(${this.themeVarService.search.default.background}) !important;
      ${this.commonPage.headerStyles()}
    }
    
    /* 搜索分类 */
    #so-nav-container {
      ${this.commonPage.tabStyles()}
    }
    
    /* 搜索提示 */
    #filter {
      margin: 0 auto !important;
      
      .filter-total, .filter-recommend a {
        color: var(${this.themeVarService.search.default.text}) !important;
      }
      
      .filter-recommend a {
        &:hover {
          color: var(${this.themeVarService.link.default.text}) !important;
        }
      }
    }
    
    #wrapper {
      #container {
        padding: 0 !important;

        #main {
          width: 100% !important;

          /* 搜索结果列表 */
          #news {
            ${this.searchCommonStyles.resultContainerStyles()}
            margin-bottom: ${this.themeVarService.search.resultItem.gap} !important;
            
            /* 搜索结果项 */
            > li {
              ${this.searchCommonStyles.resultItemStyles({ noMarginBottom: true })}
              
              &:hover {
                text-decoration: none !important;
              }
              
              /* 标题 */
              ${this.searchCommonStyles.titleStyles('> a')}
              
              /* 描述 */
              .summary {
                color: var(${this.themeVarService.search.default.text}) !important;
              }
            }
          }
          
          /* 相关搜索 */
          #bd_related {
            #rs {
              display: flex !important;
              justify-content: center !important;

              .rs-table {
                width: auto !important;
                
                a {
                  color: var(${this.themeVarService.link.default.text}) !important;
                  text-decoration: none !important;
                  
                  &:hover {
                    color: var(${this.themeVarService.link.default.textHover}) !important;
                    text-decoration: none !important;
                  }
                }
              }
            }
          }
          
          /* 搜索分页 */
          #page {
            text-align: center !important;
            margin-bottom: 20px !important;
          }
          
          /* 智能推荐 */
          .info-stream {
            .info-flow-tips {
              color: var(${this.themeVarService.search.default.text}) !important;
            }
            
            /* 推荐列表 */
            #info-flow-box {
              display: grid !important;
              grid-template-columns: repeat(1, auto);
              justify-content: center !important;
              gap: ${this.themeVarService.search.resultItem.gap} !important;
              
              /* 推荐项 */
              > li {
                position: relative !important;
                background: var(${this.themeVarService.search.resultItem.background}) !important;
                box-shadow: var(${this.themeVarService.search.resultItem.shadow}) !important;
                border-radius: ${this.themeVarService.search.resultItem.borderRadius} !important;
                padding: 15px !important;
                
                .info-flow-t {
                  color: var(${this.themeVarService.search.default.text}) !important;
                }
                
                
                .info-flow-img-box-small {
                  background-color: transparent !important;

                  .info-flow-img {
                    background-color: transparent !important;
                  }
                }
              }
            }
          }
        }
      }
      
      /* 页脚 */
      ${this.commonPage.footStyles()}
    }
    `
  }
}
